import { invoke } from "@tauri-apps/api/core";
import type {
  SipConfig,
  TransportType,
  RegistrationStatus,
  ConnectionTestResult,
  NetworkStatus,
  AudioDevices,
  AudioLevels,
  AudioDeviceInfo,
  AudioStreamConfig,
  AudioStreamSettings,
  AudioQualitySettings,
  AudioQualityMetrics,
  AppConfig,
} from "../types";

// SIP Account Management
export const login = async (config: SipConfig): Promise<void> => {
  return await invoke("login", { config });
};

export const logout = async (): Promise<void> => {
  return await invoke("logout");
};

export const getRegistrationStatus = async (): Promise<RegistrationStatus> => {
  return await invoke("get_registration_status");
};

export const testConnection = async (
  server: string,
  port: number,
  transport: TransportType
): Promise<ConnectionTestResult> => {
  return await invoke("test_connection", { server, port, transport });
};

export const getNetworkStatus = async (): Promise<NetworkStatus> => {
  return await invoke("get_network_status");
};

export const getRetryCount = async (): Promise<number> => {
  return await invoke("get_retry_count");
};

export const getMaxRetries = async (): Promise<number> => {
  return await invoke("get_max_retries");
};

export const forceRetryRegistration = async (): Promise<void> => {
  return await invoke("force_retry_registration");
};

export const getLastError = async (): Promise<string | null> => {
  return await invoke("get_last_error");
};

export const getAuthFailureCount = async (): Promise<number> => {
  return await invoke("get_auth_failure_count");
};

export const getMaxAuthFailures = async (): Promise<number> => {
  return await invoke("get_max_auth_failures");
};

export const resetAuthFailures = async (): Promise<void> => {
  return await invoke("reset_auth_failures");
};

export const isAuthenticationLocked = async (): Promise<boolean> => {
  return await invoke("is_authentication_locked");
};

// Call Management
export const makeCall = async (number: string): Promise<string> => {
  return await invoke("make_call", { number });
};

export const answerCall = async (callId: string): Promise<void> => {
  return await invoke("answer_call", { callId });
};

export const rejectCall = async (callId: string): Promise<void> => {
  return await invoke("reject_call", { callId });
};

export const hangupCall = async (callId: string): Promise<void> => {
  return await invoke("hangup_call", { callId });
};

export const muteCall = async (callId: string, muted: boolean): Promise<void> => {
  return await invoke("mute_call", { callId, muted });
};

export const getActiveCalls = async (): Promise<Record<string, any>> => {
  return await invoke("get_active_calls");
};

export const getCallInfo = async (callId: string): Promise<any | null> => {
  return await invoke("get_call_info", { callId });
};

// Audio Device Management
export const getAudioDevices = async (): Promise<AudioDevices> => {
  return await invoke("get_audio_devices");
};

export const setAudioDevice = async (
  inputDevice?: string,
  outputDevice?: string
): Promise<void> => {
  return await invoke("set_audio_device", { inputDevice, outputDevice });
};

export const getAudioLevels = async (): Promise<AudioLevels> => {
  return await invoke("get_audio_levels");
};

export const getDetailedAudioDevices = async (): Promise<AudioDeviceInfo[]> => {
  return await invoke("get_detailed_audio_devices");
};

export const setAudioStreamConfig = async (config: AudioStreamConfig): Promise<void> => {
  return await invoke("set_audio_stream_config", { config });
};

export const getAudioStreamConfig = async (): Promise<AudioStreamConfig> => {
  return await invoke("get_audio_stream_config");
};

export const updateAudioLevels = async (inputLevel: number, outputLevel: number): Promise<void> => {
  return await invoke("update_audio_levels", { inputLevel, outputLevel });
};

// Enhanced Audio Stream Management
export const createAudioStreamWithQuality = async (
  callId: string,
  settings: AudioStreamSettings,
  qualitySettings: AudioQualitySettings
): Promise<void> => {
  return await invoke("create_audio_stream_with_quality", { callId, settings, qualitySettings });
};

export const setStreamQualitySettings = async (
  callId: string,
  qualitySettings: AudioQualitySettings
): Promise<void> => {
  return await invoke("set_stream_quality_settings", { callId, qualitySettings });
};

export const getStreamQualitySettings = async (
  callId: string
): Promise<AudioQualitySettings | null> => {
  return await invoke("get_stream_quality_settings", { callId });
};

export const getCallQualityMetrics = async (
  callId: string
): Promise<AudioQualityMetrics | null> => {
  return await invoke("get_call_quality_metrics", { callId });
};

export const processIncomingRtp = async (
  callId: string,
  rtpData: number[]
): Promise<void> => {
  return await invoke("process_incoming_rtp", { callId, rtpData });
};

export const getOutgoingRtp = async (
  callId: string
): Promise<number[] | null> => {
  return await invoke("get_outgoing_rtp", { callId });
};

export const getActiveAudioStreams = async (): Promise<string[]> => {
  return await invoke("get_active_audio_streams");
};

// Configuration Management
export const loadConfig = async (): Promise<AppConfig> => {
  return await invoke("load_config");
};

export const saveConfig = async (config: AppConfig): Promise<void> => {
  return await invoke("save_config", { config });
};

export const exportConfig = async (): Promise<string> => {
  return await invoke("export_config");
};

export const importConfig = async (base64Data: string): Promise<void> => {
  return await invoke("import_config", { base64Data });
};

export const configExists = async (): Promise<boolean> => {
  return await invoke("config_exists");
};